#pragma once
#include "SolarThermalShow.h"

#define EVENT_BUTTONANI_TIMERID_BASE (UIMSG_USER + 170)
#define EVENT_BUTTONANI_PERIOD_TIMERID (EVENT_BUTTONANI_TIMERID_BASE + 1)
#define EVENT_BUTTONANI_NEXTACTION_TIMERID (EVENT_BUTTONANI_TIMERID_BASE + 2)

#define EVENT_BUTTONANI_PERIOD_DEFAULT_DURATION (500)
#define EVENT_BUTTONANI_NEXTACTION_DEFAULT_DURATION (5000)

class CButtonAniUI : public CButtonUI, public SolarThermalShow
{
	DECLARE_DUICONTROL(CButtonAniUI)
public:
	CButtonAniUI()
	{
	}
	virtual ~CButtonAniUI()
	{
	}

#define CButtonAniUI_CLASS_NAME _T("ButtonAni")
	LPCTSTR GetClass() const
	{
		return CButtonAniUI_CLASS_NAME;
	}
	LPVOID GetInterface(LPCTSTR pstrName)
	{
		if (_tcscmp(pstrName, CButtonAniUI_CLASS_NAME) == 0)
			return static_cast<CButtonAniUI *>(this);
		return CLabelUI::GetInterface(pstrName);
	}

	virtual void DoEvent(TEventUI &event)
	{
		CButtonUI::DoEvent(event);

		if (event.Type == UIEVENT_TIMER)
		{
			OnTimer((UINT_PTR)event.wParam);
		}
	}

	virtual void OnTimer(UINT_PTR idEvent)
	{
		if (idEvent == EVENT_BUTTONANI_PERIOD_TIMERID)
		{
			DrawAniFrame();
			this->Invalidate();
		}
		else if (idEvent == EVENT_BUTTONANI_NEXTACTION_TIMERID)
		{
			ShowComplete();
		}
	}

	virtual void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		CLabelUI::SetAttribute(pstrName, pstrValue);

		if (_tcsicmp(pstrName, _T("period")) == 0)
			SetPeriod(_ttoi(pstrValue));
		else if (_tcsicmp(pstrName, _T("nextact")) == 0)
			SetNextActInterval(_ttoi(pstrValue));
		else if (_tcsicmp(pstrName, _T("actimage")) == 0)
			SetActImage(pstrValue);
		else if (_tcsicmp(pstrName, _T("bkimage")) == 0)
			SetNormalImage(pstrValue);
	}

	void SetPeriod(int nValue)
	{
		m_nPeriod = nValue;
	}

	INT GetPeriod()
	{
		return m_nPeriod;
	}

	void SetNextActInterval(int nValue)
	{
		m_nInterval = nValue;
	}

	INT GetNextActInterval()
	{
		return m_nInterval;
	}

	void SetActImage(LPCTSTR pStrImage)
	{
		m_sActImage = pStrImage;
		Invalidate();
	}

	LPCTSTR GetActImage()
	{
		return m_sActImage;
	}

	void SetNormalImage(LPCTSTR pStrImage)
	{
		m_sNormalImage = pStrImage;
		Invalidate();
	}

	LPCTSTR GetNormalImage()
	{
		return m_sNormalImage;
	}

	virtual void ToActFlowAnim()
	{
		ToPlayPeriod();
		if (m_NextPlayer.size() != 0)
		{
			m_bIsNextMode = true;
			m_pManager->SetTimer(this, EVENT_BUTTONANI_NEXTACTION_TIMERID, m_nInterval);
		}
		else
		{
			m_bIsNextMode = false;
		}
	}
	virtual void ToResetFlowAnim()
	{
		m_pManager->KillTimer(this, EVENT_BUTTONANI_NEXTACTION_TIMERID);
		ToStopPeriod();
		SolarThermalShow::Reset();
		Reset();
		this->Invalidate();
	}
	virtual void ToStopFlowAnim()
	{
		m_pManager->KillTimer(this, EVENT_BUTTONANI_NEXTACTION_TIMERID);
		ToStopPeriod();
		Reset();
		this->Invalidate();
	}
	virtual void SetNextAnimPlayer(SolarThermalShow *next)
	{
		m_NextPlayer.insert(next);
	}
	virtual void SetNextStopPlayer(SolarThermalShow *next)
	{
		m_StopPlayer.insert(next);
	}
	virtual void ShowComplete()
	{
		m_pManager->KillTimer(this, EVENT_BUTTONANI_NEXTACTION_TIMERID);

		for (set<SolarThermalShow *>::iterator iter = m_NextPlayer.begin(); iter != m_NextPlayer.end(); iter++)
		{
			if (m_bIsNextMode)
			{
				(*iter)->ToActFlowAnim();
			}
		}
		for (set<SolarThermalShow *>::iterator iter = m_StopPlayer.begin(); iter != m_StopPlayer.end(); iter++)
		{
			if (m_bIsNextMode)
			{
				(*iter)->ToStopFlowAnim();
			}
		}
	}

public:
	void ToPlayPeriod()
	{
		m_pManager->SetTimer(this, EVENT_BUTTONANI_PERIOD_TIMERID, m_nPeriod);
	}
	void ToStopPeriod()
	{
		m_pManager->KillTimer(this, EVENT_BUTTONANI_PERIOD_TIMERID);
	}
	void Reset()
	{
		SetBkImage(GetNormalImage());
	}

private:
	void DrawAniFrame()
	{
		m_pManager->KillTimer(this, EVENT_BUTTONANI_PERIOD_TIMERID);

		/// to do
		SetBkImage(GetActImage());

		/// to period act:
		//m_pManager->SetTimer(this, EVENT_BUTTONANI_PERIOD_TIMERID, m_nPeriod);
	}

private:
	INT m_nPeriod = EVENT_BUTTONANI_PERIOD_DEFAULT_DURATION;
	INT m_nInterval = EVENT_BUTTONANI_NEXTACTION_DEFAULT_DURATION;
	CDuiString m_sNormalImage = _T("");
	CDuiString m_sActImage = _T("");
};
